<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
*	BIBTEX COINS export class
*
*	$Header: /cvsroot/wikindx/wikindx3/core/file/export/COINS.php,v 1.2 2007/10/21 15:26:10 sirfragalot Exp $
*****/
class COINS
{
// Constructor
	function COINS($db)
	{
		$this->db = $db;
		include_once("core/file/export/EXPORTCOMMON.php");
		$this->common = new EXPORTCOMMON($this->db);
		include_once("core/file/COINSMAP.php");
		$this->map = new COINSMAP();
	}
// display coins data for one resource (from resource lists)
	function coinsDisplay($row)
	{
		$this->entry = $this->authors = array();
		$genre = $authors = $entry = $url = $doi = FALSE;
		$this->getData($row);
		$type = $this->map->types[$row['type']];
		if($this->map->genres[$row['type']])
			$genre = "&amp;rft.genre=" . urlencode($this->map->genres[$row['type']]);
		if(!empty($this->entry))
			$entry = $this->convertEntry();
		if(!empty($this->authors))
			$authors = $this->convertEntryAuthors();
		if(array_key_exists('url', $row) && $row['url'])
			$url = '&amp;rft_id=' . urlencode($row['url']);
		if(array_key_exists('doi', $row) && $row['doi'])
			$doi = '&amp;rft_id=info:doi/' . urlencode($row['doi']);
		global $_SERVER;
		$sid ="&amp;rfr_sid=info:sid/" . 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
		return "<span " . $this->map->coinsBase . "$type" . "$genre" . 
			$entry . $authors . $url . $doi . $sid . "\"></span>";
	}
// Convert raw array of data to appropriate coins format
	function convertEntry()
	{
		$array = array();
		array_map(array($this, "uEncode"), $this->entry);
		foreach($this->entry as $key => $value)
			$array[] = "rft." . $key . "=" . $value;
		return "&amp;" . join("&amp;", $array);
	}
// Convert raw array of authors to appropriate coins format
	function convertEntryAuthors()
	{
		$array = array();
		array_map(array($this, "uEncode"), $this->authors);
		foreach($this->authors as $value)
			$array[] = "rft.au=" . $value;
		return "&amp;" . join("&amp;", $array);
	}
	function uEncode($element)
	{
		return urlencode($element);
	}
// format data
	function getData($row)
	{
		$this->entry[$this->map->{$row['type']}['title']] = urlencode($this->common->titleFormat($row));
		if((!array_key_exists('creator1', $row) || !$row['creator1']) && array_key_exists('creator2', $row)) // edited book etc.
			$row['creator1'] = $row['creator2'];
		if(array_key_exists('creator1', $row) && $row['creator1'])
			$this->nameFormat($row['creator1']);
		if((!array_key_exists('year1', $row) || !$row['year1']) && array_key_exists('year2', $row) && $row['year2'])
			$row['year1'] = $row['year2'];
		if(array_key_exists('year1', $row) && $row['year1'])
			$this->dateFormat($row);
//print_r($row); print "<P>";
// Add everything else
		foreach($this->map->{$row['type']} as $key => $value)
			{
				if(!array_key_exists($key, $row) || !$row[$key])
					continue;
				if($key == 'title')
					continue;
				$this->entry[$value] = urlencode(stripslashes($row[$key]));
			}
	}
// format names.
	function nameFormat($field)
	{
		$creatorIds = explode(",", $field);
		foreach($creatorIds as $id)
			$condition[] = $this->db->formatField('id') . "=" . $this->db->tidyInput($id);
		$recordset = $this->db->select(array('WKX_creator'), 
			array('surname', 'firstname', 'initials', 'prefix', 'id'), 
			" WHERE " . implode(' OR ', $condition));
		while($creatorRow = $this->db->loopRecordSet($recordset))
		{
			$name = $this->common->formatName($creatorRow, 'bibtex'); // use bibTeX format
			if($name)
				$this->authors[] = $name;
		}
	}
// Create date
	function dateFormat($row)
	{
		if($row['type'] == 'web_article')
		{
			$startDay = array_key_exists('miscField5', $row) ? $row['miscField5'] : FALSE;
			$startMonth = array_key_exists('miscField6', $row) ? $row['miscField6'] : FALSE;
		}
		else
		{
			$startDay = array_key_exists('miscField2', $row) ? $row['miscField2'] : FALSE;
			$startMonth = array_key_exists('miscField3', $row) ? $row['miscField3'] : FALSE;
		}
		$startDay = ($startDay == 0) ? FALSE : $startDay;
		$startMonth = ($startMonth == 0) ? FALSE : $startMonth;
		if(!$startMonth)
			$date = $row['year1'];
		else if(!$startDay)
		{
			if($startMonth < 10)
				$startMonth = '0' . $startMonth;
			$date = $row['year1'] . '-' . $startMonth;
		}
		else
		{
			if($startMonth < 10)
				$startMonth = '0' . $startMonth;
			if($startDay < 10)
				$startDay = '0' . $startDay;
			$date = $row['year1'] . '-' . $startMonth . '-' . $startDay;
		}
		$this->entry['date'] = $date;
	}
}
?>
